package lupos;

import java.io.File;
import java.io.FileWriter;
import java.util.List;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.NodeList;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import output.LexEntry;
import ontology.Ontology;
import java.io.IOException;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;


/** 
 * The first role of this class is to PARSE the root element of a 
 * FrameNet 1.3 annotation file (i.e. one of the lu***.xml files
 * in the luPosXML directory. The structure of these elements is 
 * as follows:
 * 
 *   lexunit-annotation @ID @name @frame @pos @incorporatedFE
 *     > definition #PCDATA
 *     > lexemes
 *         > lexeme*
 *     > subcorpus @name
 *         > annotationSet*
 *         
 * The MAIN() method reads each annotation file in turn, converts 
 * it into a LexEntry object, and prints the object out as an XML 
 * lexical entry file with the same name.
 */

public class LexUnitAnnotation {

	public String id;
	public String name;
	public String frame;
	public String pos;
	public String incorporatedFE;
	public String definition;
	public List<Lexeme> lexemes = new ArrayList<Lexeme>(); 
	public List<AnnotationSet> annotationSets = new ArrayList<AnnotationSet>();
	
	public LexUnitAnnotation(Element e) {
		id = e.getAttribute("ID");
		name = e.getAttribute("name");		
		frame = e.getAttribute("frame");
		pos = e.getAttribute("pos");
		incorporatedFE = e.getAttribute("incorporatedFE");
		NodeList ns = e.getChildNodes();
		for (int i=0;i<ns.getLength();i++) {
			Node n = ns.item(i);
			String s = n.getNodeName();
			if (s.equals("definition")) {
				definition = ((Element) n).getTextContent();
			}
			else if (s.equals("lexemes")) {
				NodeList ps = ((Element) n).getChildNodes();
				for (int j=0;j<ps.getLength();j++) {
					Node p = ps.item(j);
					if (p.getNodeName().equals("lexeme")) {
						lexemes.add(new Lexeme((Element) p));
					}
				}
			}
			else if (s.equals("subcorpus")) {
				Element f = (Element) n;
				NodeList ps = f.getChildNodes();
				for (int j=0;j<ps.getLength();j++) {
					Node p = ps.item(j);
					if (p.getNodeName().equals("annotationSet")) {
						// pass subcorpus name down into annotation set, for lookup:
						annotationSets.add(new AnnotationSet((Element) p,f.getAttribute("name")));
					}
				}
			}				
		}
	}

	public static void main(String[] args) throws IOException, ParserConfigurationException, SAXException {
		/* arg[0] is the FrameNet 1.3 distribution top level directory
		   e.g. /group/corpora/public/framenet/1.3
		   arg[1] is the directory you want the entry files to go into
		   e.g. /home/mmcconvi/boo */
		File frs = new File(new File(args[0],"frXML"),"frames.xml"); // frames file
		File rls = new File(new File(args[0],"frXML"),"frRelation.xml"); // relations file
		Ontology o = new Ontology(DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(frs).getDocumentElement(),DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(rls).getDocumentElement());
		FileWriter index = new FileWriter(new File(args[1],"index.html")); // make index file 
		index.write("<html>\n  <head>\n    <title>FrameNet harvested lexicon</title>\n  </head>\n  <body>\n    <table border=\"1\">\n");
        // repeat for every annotation file in the corpus:
		for (File f: (new File(args[0],"luPosXML")).listFiles()) {
			if (!f.getName().equals("lu6611.xml")) {
				System.out.println(f.getName());
				LexUnitAnnotation a = new LexUnitAnnotation(DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(f).getDocumentElement());     
				// ONLY PROCESS VERBS WITH AT LEAST ONE ANNOTATED EXAMPLE:
				if (a.pos.equals("V") && !a.annotationSets.isEmpty()) {
					// THIS IS THE IMPORTANT LINE:
					LexEntry e = new LexEntry(a,o,args[0]); // convert annotation file to lexical entry file
					FileWriter oot = new FileWriter(new File(args[1],f.getName()));
					oot.write(e.toXML());
					oot.close();	
					index.write("      <tr><td>"+e.fnName+"</td><td>"+e.semanticType.name+"</td><td><a href=\"lu"+e.fnId+".xml\">"+e.fnId+"</a></td></tr>\n");
				}
			}
		}
		index.write("    </table>\n  </body>\n</html>");
		index.close();
	}
	
}
